package applets.simple;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.text.DateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 *
 * @author lom
 */
public class HelloWorldClock extends Applet {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	StringBuffer buffer;

    @Override
    public void init() {
        buffer = new StringBuffer();
        addItem("initializing... ");
        new Timer().schedule (new TimerTask()
        {
            public void run()
            {
                repaint();
            }
        }, 1000, 1000);
    }

    @Override
    public void start() {
        addItem("starting... ");
    }

    @Override
    public void stop() {
        addItem("stopping... ");
    }

    @Override
    public void destroy() {
        addItem("preparing for unloading...");
    }

    private void addItem(String newWord) {
        System.out.println(newWord);
        buffer.append(newWord);
        repaint();
    }

    public void paint(Graphics g) {
        //Draw a Rectangle around the applet's display area.
        g.drawRect(0, 0,
                getWidth() - 1,
                getHeight() - 1);

        g.setColor (Color.BLACK);
        g.fillRect (0, 0, getWidth(), getHeight());

        g.setColor (Color.WHITE);

        Date now = new Date();
        DateFormat df = DateFormat.getTimeInstance();
        g.drawString (df.format (now), 20, 15);
        //Draw the current string inside the rectangle.
        //g.drawString(buffer.toString(), 5, 15);
    }
}
